package com.example.demo.alarm.service.impl;

import com.example.demo.alarm.dao.AlarmTactics;
import com.example.demo.alarm.dao.AlarmTacticsSensor;
import com.example.demo.alarm.mapper.AlarmTacticsMapper;
import com.example.demo.alarm.service.AlarmTacticsService;
import com.example.demo.scheduled.MyTask;
import com.example.demo.scheduled.ScheduleTask;
import com.example.demo.scheduled.ScheduleUtil;
import com.example.demo.system.utility.PageInfo;
import com.example.demo.system.utility.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AlarmTacticsServiceImpl implements AlarmTacticsService {

    @Autowired
    private AlarmTacticsMapper alarmTacticsMapper;

    //获取报警策略集合
    @Override
    public Result getList(Integer pageNum, Integer pageSize, String name, String healthOrComfort) {
        Integer start = PageInfo.start(pageNum, pageSize);
        //获取报警策略集合
        List<AlarmTactics> list = alarmTacticsMapper.getList(start, pageSize, name, healthOrComfort);
        //获取每个报警策略下的关联的传感器集合
        for (AlarmTactics alarmTactics : list) {
            alarmTactics.setAlarmTacticsSensor(alarmTacticsMapper.getSensorList(alarmTactics.getId()));
        }
        Integer listCount = alarmTacticsMapper.getListCount(name, healthOrComfort);
        PageInfo info = new PageInfo(pageNum, pageSize, listCount, list);
        return Result.success(info);
    }

    //获取报警策略详情
    @Override
    public Result get(Integer id) {
        AlarmTactics alarmTactics = alarmTacticsMapper.get(id);
        alarmTactics.setAlarmTacticsSensor(alarmTacticsMapper.getSensorList(id));
        return Result.success(alarmTactics);
    }

    @Override
    public Result add(AlarmTactics alarmTactics) {
        //新增报警策略
        alarmTacticsMapper.add(alarmTactics);

        List<AlarmTacticsSensor> alarmTacticsSensor = alarmTactics.getAlarmTacticsSensor();
        //新增报警策略关联传感器
        for (AlarmTacticsSensor sensor : alarmTacticsSensor) {
            //报警id
            sensor.setTacticsId(alarmTactics.getId());
            alarmTacticsMapper.addSensorList(sensor);
        }

        //新建执行任务
        ScheduleTask helloTask1 = new MyTask(alarmTactics.getId());
        //调用定时任务方法
        ScheduleUtil.start1(helloTask1, Long.valueOf(alarmTactics.getFrequency()) * 1000);

        return Result.success("新增成功");
    }

    @Override
    public Result update(AlarmTactics alarmTactics) {
        //修改报警策略
        alarmTacticsMapper.update(alarmTactics);

        List<AlarmTacticsSensor> alarmTacticsSensor = alarmTactics.getAlarmTacticsSensor();
        //修改报警策略关联传感器
        //无法新增或删除关联传感器，只能修改数值
        for (AlarmTacticsSensor sensor : alarmTacticsSensor) {
            alarmTacticsMapper.updateSensorList(sensor);
        }

        //新建执行任务
        ScheduleTask helloTask1 = new MyTask(alarmTactics.getId());
        //判断报警策略是否开启
        if ("开启".equals(alarmTactics.getState())) {
            //调用定时任务方法
            ScheduleUtil.start1(helloTask1, Long.valueOf(alarmTactics.getFrequency()) * 1000);
        } else {
            //停止定时任务
            ScheduleUtil.cancel(helloTask1);
        }
        return Result.success("修改成功");
    }

    @Override
    public Result delete(Integer id) {
        //删除报警策略
        alarmTacticsMapper.delete(id);
        //删除关联传感器集合
        alarmTacticsMapper.deleteSensorList(id);
        return Result.success("删除成功");
    }

    @Override
    public Result deleteSensor(Integer sensorId) {
        //删除单个关联传感器
        alarmTacticsMapper.deleteSensor(sensorId);
        return Result.success("删除成功");
    }


}
